home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / iterator.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  28.5 KB  |  913 lines

  1. #ifndef __STD_ITERATOR__
  2. #define __STD_ITERATOR__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * iterator - iterator declarations for the Standard Library
  8.  *
  9.  * $Id: iterator,v 1.70 1996/09/27 08:02:52 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58.  
  59. #include <stdcomp.h>
  60. #include <rw/stddefs.h>
  61.  
  62. #ifndef _RWSTD_NO_NEW_HEADER
  63. #include <cstddef>
  64. #else
  65. #include <stddef.h>
  66. #endif
  67.  
  68. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  69. #define _RWSTD_VALUE_TYPE(a) value_type(*(a))
  70. #else
  71. #define _RWSTD_VALUE_TYPE(a) value_type(a)
  72. #endif
  73.  
  74. #ifndef _RWSTD_NO_NAMESPACE
  75. namespace std {
  76. #endif
  77.  
  78. //
  79. // Standard iterator tags.
  80. //
  81.   
  82. struct input_iterator_tag
  83. {
  84.     input_iterator_tag() {;}
  85. };
  86.  
  87. struct output_iterator_tag
  88. {
  89.     output_iterator_tag() {;}
  90. };
  91.  
  92. struct forward_iterator_tag : public input_iterator_tag
  93. {
  94.     forward_iterator_tag() {;}
  95. };
  96.  
  97. struct bidirectional_iterator_tag : public forward_iterator_tag
  98. {
  99.     bidirectional_iterator_tag() {;}
  100. };
  101.  
  102. struct random_access_iterator_tag : public bidirectional_iterator_tag
  103. {
  104.     random_access_iterator_tag() {;}
  105. };
  106.  
  107.  
  108. //
  109. // Basic iterators.
  110. //
  111.  
  112. //
  113. // Note that _RWSTD_SIMPLE_DEFAULT(x)
  114. // will expand to: ' = x', or nothing,
  115. // depending on your compiler's capabilities and/or
  116. // flag settings (see stdcomp.h).
  117. //
  118. template <class Category, class T,  
  119.           class Distance _RWSTD_SIMPLE_DEFAULT(ptrdiff_t)>
  120. struct iterator
  121. {
  122.    typedef T value_type;
  123.    typedef Distance distance_type;
  124.    typedef Category iterator_category;
  125. };
  126.  
  127. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  128.  
  129. template <class Iterator> struct iterator_traits
  130. {
  131.    typedef _TYPENAME Iterator::value_type value_type;
  132.    typedef _TYPENAME Iterator::distance_type distance_type;
  133.    typedef _TYPENAME Iterator::iterator_category iterator_category;
  134. };
  135. template <class T> struct iterator_traits<T*>
  136. {
  137.    typedef T value_type;
  138.    typedef ptrdiff_t distance_type;
  139.    typedef random_access_iterator_tag iterator_category;
  140. };
  141.  
  142. template <class ForwardIterator>
  143. inline iterator_traits<ForwardIterator>::distance_type
  144. distance (ForwardIterator first, ForwardIterator last)
  145. {
  146.     iterator_traits<ForwardIterator>::distance_type n = 0;
  147.     __distance(first, last, n, 
  148.                iterator_traits<ForwardIterator>::iterator_category);
  149.     return n;
  150. }
  151.  
  152. template <class ForwardIterator, class Distance>
  153. inline void advance (ForwardIterator& i, Distance n)
  154. {
  155.     __advance(i, n, iterator_traits<ForwardIterator>::iterator_category);
  156. }
  157.  
  158. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  159.  
  160.  
  161.  
  162. template <class T > 
  163. struct input_iterator : public iterator<input_iterator_tag,T,ptrdiff_t>
  164. {
  165. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  166.     T* operator* () { return _RWSTD_STATIC_CAST(T*,0); } 
  167. #endif
  168. };
  169.  
  170. struct output_iterator : public iterator<output_iterator_tag,void,void>
  171. {
  172.     typedef output_iterator_tag iterator_category;
  173. };
  174.  
  175. template <class T, class Distance _RWSTD_SIMPLE_DEFAULT(ptrdiff_t) > 
  176. struct forward_iterator : public iterator<forward_iterator_tag,T,Distance>
  177. {
  178. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  179.     T* operator* () { return _RWSTD_STATIC_CAST(T*,0); } 
  180. #endif
  181. };
  182.  
  183. template <class T, class Distance _RWSTD_SIMPLE_DEFAULT(ptrdiff_t)> 
  184. struct bidirectional_iterator : public iterator<bidirectional_iterator_tag,T,Distance>
  185. {
  186. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  187.     T* operator* () { return _RWSTD_STATIC_CAST(T*,0); } 
  188. #endif
  189. };
  190.  
  191. template <class T, class Distance _RWSTD_SIMPLE_DEFAULT(ptrdiff_t)> 
  192. struct random_access_iterator : public iterator<random_access_iterator_tag,T,Distance>
  193. {
  194. #ifdef _RWSTD_NO_BASE_CLASS_MATCH
  195.     T* operator* () { return _RWSTD_STATIC_CAST(T*,0); } 
  196. #endif
  197. };
  198.  
  199.  
  200. //
  201. // Iterator category.
  202. //
  203.  
  204. template <class T>
  205. inline input_iterator_tag 
  206. iterator_category (const input_iterator<T>&)
  207. {
  208.     return input_iterator_tag();
  209. }
  210.  
  211. inline output_iterator_tag iterator_category (const output_iterator&)
  212. {
  213.     return output_iterator_tag();
  214. }
  215.  
  216. template <class T, class Distance> 
  217. inline forward_iterator_tag
  218. iterator_category (const forward_iterator<T, Distance>&)
  219. {
  220.     return forward_iterator_tag();
  221. }
  222.  
  223. template <class T, class Distance>
  224. inline bidirectional_iterator_tag
  225. iterator_category (const bidirectional_iterator<T, Distance>&)
  226. {
  227.     return bidirectional_iterator_tag();
  228. }
  229.  
  230. template <class T, class Distance> 
  231. inline random_access_iterator_tag
  232. iterator_category (const random_access_iterator<T, Distance>&)
  233. {
  234.     return random_access_iterator_tag();
  235. }
  236.  
  237.  
  238. template <class T>
  239. inline random_access_iterator_tag iterator_category (const T*)
  240. {
  241.     return random_access_iterator_tag();
  242. }
  243.  
  244. //
  245. // Value type.
  246. //
  247.  
  248. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  249. template <class T> 
  250. inline T* value_type (const input_iterator<T>&)
  251. {
  252.   return _RWSTD_STATIC_CAST(T*,0);
  253. }
  254.  
  255. template <class T, class Distance> 
  256. inline T* value_type (const forward_iterator<T, Distance>&)
  257. {
  258.   return _RWSTD_STATIC_CAST(T*,0);
  259. }
  260.  
  261. template <class T, class Distance> 
  262. inline T* value_type (const bidirectional_iterator<T, Distance>&)
  263. {
  264.   return _RWSTD_STATIC_CAST(T*,0);
  265. }
  266.  
  267. template <class T, class Distance> 
  268. inline T* value_type (const random_access_iterator<T, Distance>&)
  269. {
  270.   return _RWSTD_STATIC_CAST(T*,0);
  271. }
  272.  
  273. template <class T>
  274. inline T* value_type (const T*)
  275. {
  276.   return _RWSTD_STATIC_CAST(T*,0);
  277. }
  278. #else
  279. template <class T>
  280. inline T* value_type (const T) 
  281. {
  282.   return _RWSTD_STATIC_CAST(T*,0);
  283. }
  284. #endif
  285.  
  286. //
  287. // Distance type.
  288. //
  289.  
  290. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  291. template <class T, class Distance> 
  292. inline Distance* distance_type (const forward_iterator<T, Distance>&)
  293. {
  294.   return _RWSTD_STATIC_CAST(Distance*,0);
  295. }
  296.  
  297. template <class T, class Distance> 
  298. inline Distance* 
  299. distance_type (const bidirectional_iterator<T, Distance>&)
  300. {
  301.   return _RWSTD_STATIC_CAST(Distance*,0);
  302. }
  303.  
  304. template <class T, class Distance>
  305. inline Distance* 
  306. distance_type (const random_access_iterator<T, Distance>&)
  307. {
  308.   return _RWSTD_STATIC_CAST(Distance*,0);
  309. }
  310.  
  311. template <class T>
  312. inline ptrdiff_t* distance_type (const T*)
  313.   return _RWSTD_STATIC_CAST(ptrdiff_t*,0);
  314. }
  315. #else
  316. template <class T>
  317. inline ptrdiff_t* distance_type (const T)  
  318.   return _RWSTD_STATIC_CAST(ptrdiff_t*,0);
  319. }
  320. #endif
  321.  
  322. //
  323. // Iterator operations.
  324. //
  325.  
  326. template <class InputIterator, class Distance>
  327. void __advance (InputIterator& i, Distance n, input_iterator_tag);
  328.  
  329. template <class ForwardIterator, class Distance>
  330. void __advance (ForwardIterator& i, Distance n, forward_iterator_tag);
  331.  
  332. template <class BidirectionalIterator, class Distance>
  333. void __advance (BidirectionalIterator& i, Distance n, 
  334.                 bidirectional_iterator_tag);
  335.  
  336. template <class InputIterator, class Distance>
  337. void __distance (InputIterator first, InputIterator last, Distance& n, 
  338.                  input_iterator_tag);
  339.  
  340. template <class ForwardIterator, class Distance>
  341. void __distance (ForwardIterator first, ForwardIterator last, Distance& n, 
  342.                  forward_iterator_tag);
  343.  
  344. template <class BidirectionalIterator, class Distance>
  345. void __distance (BidirectionalIterator first, BidirectionalIterator last, 
  346.                  Distance& n, bidirectional_iterator_tag);
  347.  
  348. template <class RandomAccessIterator, class Distance>
  349. inline void __distance (RandomAccessIterator first, RandomAccessIterator last, 
  350.                         Distance& n, random_access_iterator_tag)
  351. {
  352.     n = last - first;
  353. }
  354.  
  355. #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
  356. template <class ForwardIterator, class Distance>
  357. inline void distance (ForwardIterator first, ForwardIterator last, Distance& n)
  358. {
  359. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  360.     __distance(first, last, n, iterator_category(first));
  361. #else
  362.     __distance(first, last, n, input_iterator_tag());
  363. #endif
  364. }
  365. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  366.  
  367.  
  368. template <class RandomAccessIterator, class Distance>
  369. inline void __advance (RandomAccessIterator& i, Distance n, 
  370.                        random_access_iterator_tag)
  371. {
  372.     i += n;
  373. }
  374.  
  375.  
  376. #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
  377. template <class ForwardIterator, class Distance>
  378. inline void advance (ForwardIterator& i, Distance n)
  379. {
  380. #ifndef _RWSTD_NO_BASE_CLASS_MATCH
  381.     __advance(i, n, iterator_category(i));
  382. #else
  383.     __advance(i, n, input_iterator_tag());
  384. #endif
  385. }
  386. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  387.  
  388. //
  389. // Reverse bidirectional iterator.
  390. //
  391.  
  392.     //
  393.     // Forward declarations.
  394.     //
  395.  
  396. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  397. template <class BidirectionalIterator, class T, class Reference,
  398.           class Pointer, class Distance> class reverse_bidirectional_iterator;
  399.  
  400. template <class BidirectionalIterator, class T, class Reference,
  401.           class Pointer, class Distance>
  402. inline bool operator== (
  403.       const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  404.                                            Pointer, Distance>& x, 
  405.       const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  406.                                            Pointer, Distance>& y);
  407. #endif
  408.  
  409. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  410. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  411. template <class BidirectionalIterator, 
  412.           class T = iterator_traits<BidirectionalIterator>::value_type, 
  413.           class Reference = T&,
  414.           class Pointer = T*, class Distance = ptrdiff_t> 
  415. #else
  416. template <class BidirectionalIterator, class T, class Reference = T&,
  417.           class Pointer = T*, class Distance = ptrdiff_t>
  418. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  419. #else
  420. template <class BidirectionalIterator, class T, class Reference, 
  421.           class Pointer, class Distance> 
  422. #endif /* _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES */
  423.  
  424. //
  425. // Reference = T& 
  426. // Distance  = ptrdiff_t
  427. // Pointer   = T*
  428. //
  429. class reverse_bidirectional_iterator
  430.     : public bidirectional_iterator<T,Distance>
  431. {
  432.     typedef reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  433.                                            Pointer, Distance> self;
  434.     friend bool operator== (const self& x, const self& y);
  435.  
  436.   protected:
  437.  
  438.     BidirectionalIterator current;
  439.  
  440.   public:
  441.  
  442.     typedef BidirectionalIterator iter_type;
  443.     typedef T value_type;
  444.     typedef Reference reference_type;
  445.     typedef Pointer pointer_type;
  446.     typedef Distance distance_type;
  447.  
  448.     reverse_bidirectional_iterator() {}
  449.     _EXPLICIT reverse_bidirectional_iterator (BidirectionalIterator x)
  450.         : current(x) {}
  451.     BidirectionalIterator base() const { return current; }
  452.     Reference operator* () const
  453.     {
  454.         BidirectionalIterator tmp = current; return *--tmp;
  455.     }
  456.     #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  457.     Pointer operator->() const { return &(operator*()); }
  458.     #endif
  459.  
  460.     self& operator++ ()    { --current; return *this;                 }
  461.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  462.     self& operator-- ()    { ++current; return *this;                 }
  463.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  464. };
  465.  
  466. template <class BidirectionalIterator, class T, class Reference,
  467.           class Pointer, class Distance>
  468. inline bool operator== (
  469.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  470.                                          Pointer, Distance>& x, 
  471.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  472.                                          Pointer, Distance>& y)
  473. {
  474.     return x.current == y.current;
  475. }
  476.  
  477. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  478. template <class BidirectionalIterator, class T, class Reference,
  479.           class Pointer, class Distance>
  480. inline bool operator!= (
  481.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  482.                                          Pointer, Distance>& x, 
  483.     const reverse_bidirectional_iterator<BidirectionalIterator, T, Reference,
  484.                                          Pointer, Distance>& y)
  485. {
  486.     return !(x == y);
  487. }
  488. #endif
  489.  
  490. //
  491. // Reverse iterator.                                 
  492. //
  493.  
  494.     //
  495.     // Forward Declarations.
  496.     //
  497. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  498. template <class RandomAccessIterator, class T, class Reference,
  499.           class Pointer, class Distance>  class reverse_iterator;
  500.  
  501. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  502. inline bool operator== (const reverse_iterator<RandomAccessIterator, T,
  503.                  Reference, Pointer, Distance>& x, 
  504.                  const reverse_iterator<RandomAccessIterator, T,
  505.                  Reference, Pointer, Distance>& y);
  506.  
  507. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  508. inline bool operator< (const reverse_iterator<RandomAccessIterator, T,
  509.                 Reference, Pointer, Distance>& x, 
  510.                 const reverse_iterator<RandomAccessIterator, T,
  511.                 Reference, Pointer, Distance>& y);
  512.  
  513. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  514. inline Distance operator- (const reverse_iterator<RandomAccessIterator, T,
  515.                     Reference, Pointer, Distance>& x, 
  516.                     const reverse_iterator<RandomAccessIterator, T,
  517.                     Reference, Pointer, Distance>& y);
  518.  
  519. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  520. inline reverse_iterator<RandomAccessIterator, T, Reference, Pointer, Distance> 
  521. operator+ (Distance n,
  522.       const reverse_iterator<RandomAccessIterator, T, Reference, Pointer, Distance>& x);
  523. #endif
  524.  
  525. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  526. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  527. template <class RandomAccessIterator, 
  528.           class T = iterator_traits<RandomAccessIterator>::value_type, 
  529.           class Reference = T&,
  530.           class Pointer = T*, class Distance = ptrdiff_t> 
  531. #else
  532. template <class RandomAccessIterator, class T, class Reference = T&,
  533.           class Pointer = T*, class Distance = ptrdiff_t>
  534. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  535. #else
  536. template <class RandomAccessIterator, class T, class Reference,
  537.           class Pointer, class Distance> 
  538. #endif /* _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES */
  539.  
  540. //
  541. // Reference = T&
  542. // Distance  = ptrdiff_t
  543. // Pointer   = T*
  544. //
  545. class reverse_iterator : public random_access_iterator<T, Distance>
  546. {
  547.     typedef reverse_iterator<RandomAccessIterator,T,Reference,Pointer,Distance> self;
  548.  
  549.     friend bool operator==    (const self& x, const self& y);
  550.     friend bool operator<     (const self& x, const self& y);
  551.     friend Distance operator- (const self& x, const self& y);
  552.     friend self operator+     (Distance n, const self& x);
  553.     
  554.   protected:
  555.  
  556.     RandomAccessIterator current;
  557.  
  558.   public:
  559.  
  560.     typedef RandomAccessIterator iter_type;
  561.     typedef T value_type;
  562.     typedef Reference reference_type;
  563.     typedef Pointer pointer_type;
  564.     typedef Distance distance_type;
  565.  
  566.     reverse_iterator() {}
  567.     _EXPLICIT reverse_iterator (RandomAccessIterator x) : current(x) {}
  568.     RandomAccessIterator base () const { return current; }
  569.     Reference operator* () const 
  570.     { RandomAccessIterator tmp = current; return *--tmp; }
  571.     #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  572.     Pointer operator->() const { return &(operator*()); }
  573.     #endif
  574.  
  575.     self& operator++ ()    { --current; return *this;                 }
  576.     self  operator++ (int) { self tmp = *this; --current; return tmp; }
  577.     self& operator-- ()    { ++current; return *this;                 }
  578.     self  operator-- (int) { self tmp = *this; ++current; return tmp; }
  579.  
  580.     self  operator+  (Distance n) const { self tmp(current - n); return tmp; }
  581.     self& operator+= (Distance n)       { current -= n; return *this;        }
  582.     self  operator-  (Distance n) const { self tmp(current + n); return tmp; }
  583.     self& operator-= (Distance n)       { current += n; return *this;        }
  584.  
  585.     Reference operator[] (Distance n) const { return *(*this + n); }
  586. };
  587.  
  588. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  589. inline bool operator== (const reverse_iterator<RandomAccessIterator, T,
  590.                         Reference, Pointer, Distance>& x, 
  591.                         const reverse_iterator<RandomAccessIterator, T,
  592.                         Reference, Pointer, Distance>& y)
  593. {
  594.     return x.current == y.current;
  595. }
  596.  
  597. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  598. inline bool operator< (const reverse_iterator<RandomAccessIterator, T,
  599.                        Reference, Pointer, Distance>& x,
  600.                        const reverse_iterator<RandomAccessIterator, T,
  601.                        Reference, Pointer, Distance>& y)
  602. {
  603.     return y.current < x.current;
  604. }
  605.  
  606. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  607. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  608. inline bool operator!= (const reverse_iterator<RandomAccessIterator, T,
  609.                         Reference, Pointer, Distance>& x, 
  610.                         const reverse_iterator<RandomAccessIterator, T,
  611.                         Reference, Pointer, Distance>& y)
  612. {
  613.     return !(x == y);
  614. }
  615.  
  616. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  617. inline bool operator> (const reverse_iterator<RandomAccessIterator, T,
  618.                        Reference, Pointer, Distance>& x,
  619.                        const reverse_iterator<RandomAccessIterator, T,
  620.                        Reference, Pointer, Distance>& y)
  621. {
  622.     return y < x;
  623. }
  624.  
  625. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  626. inline bool operator<= (const reverse_iterator<RandomAccessIterator, T,
  627.                        Reference, Pointer, Distance>& x,
  628.                        const reverse_iterator<RandomAccessIterator, T,
  629.                        Reference, Pointer, Distance>& y)
  630. {
  631.     return !(y < x);
  632. }
  633.  
  634. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  635. inline bool operator>= (const reverse_iterator<RandomAccessIterator, T,
  636.                        Reference, Pointer, Distance>& x,
  637.                        const reverse_iterator<RandomAccessIterator, T,
  638.                        Reference, Pointer, Distance>& y)
  639. {
  640.     return !(x < y);
  641. }
  642. #endif
  643.  
  644. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  645. inline Distance operator- (const reverse_iterator<RandomAccessIterator, T,
  646.                            Reference, Pointer, Distance>& x, 
  647.                            const reverse_iterator<RandomAccessIterator, T,
  648.                            Reference, Pointer, Distance>& y)
  649. {
  650.     return y.current - x.current;
  651. }
  652.  
  653. template <class RandomAccessIterator, class T, class Reference, class Pointer, class Distance>
  654. inline reverse_iterator<RandomAccessIterator, T, Reference, Pointer, Distance> 
  655. operator+ (Distance n, const reverse_iterator<RandomAccessIterator, T,
  656.            Reference, Pointer, Distance>& x)
  657. {
  658.     return reverse_iterator<RandomAccessIterator, T, Reference, Pointer, Distance>
  659.     (x.current - n);
  660. }
  661.  
  662. //
  663. // Back insert iterator.
  664. //
  665.  
  666. template <class Container>
  667. class back_insert_iterator : public output_iterator 
  668. {
  669.   protected:
  670.  
  671.     Container& container;
  672.  
  673.   public:
  674.     typedef Container container_type;
  675.     typedef _TYPENAME Container::value_type value_type;
  676.  
  677.     _EXPLICIT back_insert_iterator (Container& x) : container(x) {}
  678.     back_insert_iterator<Container>&
  679.     operator= (const _TYPENAME Container::value_type& value)
  680.     {
  681.         container.push_back(value); return *this;
  682.     }
  683.     back_insert_iterator<Container>& operator*  ()    { return *this; }
  684.     back_insert_iterator<Container>& operator++ ()    { return *this; }
  685.     back_insert_iterator<Container> operator++ (int) { return *this; }
  686. };
  687.  
  688. template <class Container>
  689. inline back_insert_iterator<Container> back_inserter (Container& x)
  690. {
  691.     return back_insert_iterator<Container>(x);
  692. }
  693.  
  694. //
  695. // Front insert iterator.
  696. //
  697.  
  698. template <class Container>
  699. class front_insert_iterator : public output_iterator 
  700. {
  701.   protected:
  702.  
  703.     Container& container;
  704.  
  705.   public:
  706.     typedef Container container_type;
  707.     typedef _TYPENAME Container::value_type value_type;
  708.  
  709.     _EXPLICIT front_insert_iterator (Container& x) : container(x) {}
  710.     front_insert_iterator<Container>&
  711.     operator= (const _TYPENAME Container::value_type& value)
  712.     { 
  713.         container.push_front(value); return *this;
  714.     }
  715.     front_insert_iterator<Container>& operator*  ()    { return *this; }
  716.     front_insert_iterator<Container>& operator++ ()    { return *this; }
  717.     front_insert_iterator<Container> operator++ (int) { return *this; }
  718. };
  719.  
  720. template <class Container>
  721. inline front_insert_iterator<Container> front_inserter (Container& x)
  722. {
  723.     return front_insert_iterator<Container>(x);
  724. }
  725.  
  726. //
  727. // Insert iterator.
  728. //
  729.  
  730. template <class Container>
  731. class insert_iterator : public output_iterator 
  732. {
  733.   private:
  734.     _TYPENAME Container::iterator iter;
  735.  
  736.   protected:
  737.     Container&                   container;
  738.  
  739.   public:
  740.     typedef Container container_type;
  741.     typedef _TYPENAME Container::value_type value_type;
  742.  
  743.     insert_iterator (Container& x, _TYPENAME Container::iterator i)
  744.         : container(x), iter(i) {}
  745.     insert_iterator<Container>&
  746.     operator= (const _TYPENAME Container::value_type& value)
  747.     { 
  748.         iter = container.insert(iter, value); ++iter; return *this;
  749.     }
  750.     insert_iterator<Container>& operator*  ()    { return *this; }
  751.     insert_iterator<Container>& operator++ ()    { return *this; }
  752.     insert_iterator<Container>& operator++ (int) { return *this; }
  753. };
  754.  
  755. template <class Container, class Iterator>
  756. inline insert_iterator<Container> inserter (Container& x, Iterator i)
  757. {
  758.     _TYPENAME Container::iterator c(i);
  759.     insert_iterator<Container> tmp(x, c);
  760.     return tmp;
  761. }
  762.  
  763. template <class charT> struct _RWSTDExportTemplate char_traits;
  764. _RWSTD_TEMPLATE struct _RWSTDExport char_traits<char>;
  765. #ifndef _RWSTD_NO_WIDE_CHAR
  766. _RWSTD_TEMPLATE struct _RWSTDExport char_traits<wchar_t>;
  767. #endif
  768.  
  769. #ifndef _RW_STD_IOSTREAM
  770.  
  771. #ifndef _RWSTD_NO_NAMESPACE
  772. }
  773. #endif
  774.  
  775. #include <iostream.h>
  776.  
  777. #ifndef _RWSTD_NO_NAMESPACE
  778. namespace std {
  779. #endif
  780.  
  781. //
  782. // Stream iterators.
  783. //
  784.  
  785. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  786. template <class T, class charT, class traits, class Distance>
  787. class istream_iterator;
  788.  
  789. template <class T, class charT, class traits, class Distance>
  790. bool operator== (const istream_iterator<T, charT,traits,Distance>& x,
  791.                  const istream_iterator<T, charT,traits,Distance>& y);
  792. #endif
  793.  
  794. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  795. template <class T, class charT, 
  796.           class traits = char_traits<charT>, 
  797.           class Distance = ptrdiff_t> 
  798. #else
  799. template <class T, class charT, class traits, class Distance>
  800. #endif
  801. class istream_iterator : public iterator<input_iterator_tag,T,void>
  802. {
  803.   friend bool operator== (const istream_iterator<T, charT,traits,Distance>& x,
  804.                           const istream_iterator<T, charT,traits,Distance>& y);
  805. protected:
  806.  
  807.     istream* stream;
  808.     T        value;
  809.     bool     end_marker;
  810.  
  811.     void read ()
  812.     {
  813.         end_marker = (*stream) ? true : false;
  814.         if (end_marker) *stream >> value;
  815.         end_marker = (*stream) ? true : false;
  816.     }
  817. public:
  818.     typedef T value_type;
  819.     typedef charT char_type;
  820.     typedef traits traits_type;
  821.     typedef istream istream_type;
  822.  
  823.     istream_iterator () : stream(&cin), end_marker(false) {}
  824.     istream_iterator (istream& s) : stream(&s) { read(); }
  825.     istream_iterator ( const istream_iterator<T,charT,traits,Distance>& x )
  826.     :stream(x.stream) , value(x.value) , end_marker(x.end_marker)
  827.     { }
  828.     const T& operator* () const { return value; }
  829.     #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  830.     const T* operator->() const { return &value; }
  831.     #endif
  832.     istream_iterator<T, charT,traits,Distance>& operator++ ()
  833.     { 
  834.         read(); return *this;
  835.     }
  836.     istream_iterator<T, charT,traits,Distance> operator++ (int)
  837.     {
  838.         istream_iterator<T, charT,traits,Distance> tmp = *this; 
  839.         read(); 
  840.         return tmp;
  841.     }
  842. };
  843.  
  844. template <class T, class charT, class traits, class Distance>
  845. inline bool operator== (const istream_iterator<T, charT,traits,Distance>& x,
  846.                         const istream_iterator<T, charT,traits,Distance>& y)
  847. {
  848.     return x.stream == y.stream && x.end_marker == y.end_marker ||
  849.            x.end_marker == false && y.end_marker == false;
  850. }
  851.  
  852. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  853. template <class T, class charT, class traits, class Distance>
  854. inline bool operator!= (const istream_iterator<T, charT,traits,Distance>& x,
  855.                         const istream_iterator<T, charT,traits,Distance>& y)
  856. {
  857.     return !(x == y);
  858. }
  859. #endif
  860.  
  861. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  862. template <class T, class charT, 
  863.           class traits = char_traits<charT> >
  864. #else
  865. template <class T, class charT, class traits>
  866. #endif
  867. class ostream_iterator : public output_iterator
  868. {
  869. protected:
  870.  
  871.     ostream* stream;
  872.     const char*    str;
  873.  
  874. public:
  875.     typedef T value_type;
  876.     typedef charT char_type;
  877.     typedef traits traits_type;
  878.     typedef ostream istream_type;
  879.  
  880.     ostream_iterator (ostream& s) : stream(&s), str(0) { ; }
  881.     ostream_iterator (ostream& s,const char* c) 
  882.       : stream(&s), str((char *)c)  { ; }
  883.     ostream_iterator ( const ostream_iterator<T,charT,traits>& x )
  884.       :stream(x.stream) , str(x.str)
  885.     { ; }
  886.     ostream_iterator<T,charT,traits>& operator= (const T& value)
  887.     { 
  888.         *stream << value;
  889.         if (str) *stream << str;
  890.         return *this;
  891.     }
  892.     ostream_iterator<T,charT,traits>& operator*  ()    { return *this; }
  893.     ostream_iterator<T,charT,traits>& operator++ ()    { return *this; } 
  894.     ostream_iterator<T,charT,traits>& operator++ (int) { return *this; }
  895. };
  896.  
  897.  
  898. #endif /* _RW_STD_IOSTREAM */
  899.  
  900.  
  901. #ifndef _RWSTD_NO_NAMESPACE
  902. }
  903. #endif
  904.  
  905. #ifdef _RWSTD_NO_TEMPLATE_REPOSITORY
  906. #include <iterator.cc>
  907. #endif
  908.  
  909. #pragma option pop
  910. #endif /* __STD_ITERATOR__ */
  911.